package com.framework
{
	import com.framework.IUpdatable;
	import com.framework.util.Log;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;

	public class BasicState extends MovieClip
	{
		private var item_list:Array = new Array();
	
		/**
		 * Methode à overrider
		 * */
		public function create():void
		{
			
		}
		public function destroy():void
		{
			Log.debug(this,"Destroy");
			for each(var item:IUpdatable in this.item_list)
			{
				item.destroy();
			}
			while(this.numChildren)
			{
				fullDestroy(this.getChildAt(0));
				this.removeChildAt(0);
			}
		}
		public function fullDestroy(e:*):void
		{
			try{
				while(e.numChildren)
				{
					finalDestroy(e.getChildAt(0));
					e.removeChildAt(0);
				}
			}catch(e:Error){
			}
		}
		public function finalDestroy(e:*):void
		{
			try{
				while(e.numChildren)
				{
					e.removeChildAt(0);
				}
			}catch(e:Error){
			}
		}

		public function update():void
		{
			for each(var item:IUpdatable in this.item_list)
			{
				item.update();
			}
		}
		public function add(obj:IUpdatable):void
		{
			Log.debug(this,"add : "+obj);
			obj.create();
			item_list.push(obj);
			this.addChild(DisplayObject(obj));
		}
		public function remove(obj:IUpdatable):void
		{
			for(var i:int = 0;item_list.length;i++)
			{
				if(item_list[i] == obj)
				{
					obj.destroy();
					obj = null;
					item_list.splice(i,1);
					break;
				}
			}
		}
	}
}